package com.dgut.admodule.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dgut.admodule.dao.ICompanyDao;
import com.dgut.admodule.entity.Company;

@Service
@Transactional
public class CompanyService implements ICompanyService{
	@Autowired
	private ICompanyDao companyDao;
	
	public void saveOrUpdate(Company company) 
	{
		companyDao.save(company);
	}

	public void delete(Company company) 
	{
		companyDao.delete(company);
	}

	public void delete(List<Long> ids) 
	{
		List<Company> companys =  (List<Company>) companyDao.findAll(ids);
		if(companys!=null) {
			companyDao.delete(companys);
		}
	}
	
	@Transactional(readOnly = true)
	public Company findOne(Long id) 
	{
		return companyDao.findOne(id);
	}

	@Transactional(readOnly = true)
	public Company findUserName(String userName) 
	{
		Company company = companyDao.findByUserName(userName);
		if(company != null) {
			return company;
		}else {
			Company company2 = new Company();
			company2.setUserName(userName);
			companyDao.save(company2);
			return company2;
		}
	}
	
	@Transactional(readOnly = true)
	public List<Company> findAll() 
	{
		return (List<Company>) companyDao.findAll();
	}
	
	@Transactional(readOnly = true)
	public Page<Company> findAll(Specification<Company> spec, Pageable pageable) 
	{
		return companyDao.findAll(spec, pageable);
	}
}
